package com.rocksea.app.db.lowStrain;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.Default;
import com.litesuits.orm.db.annotation.Ignore;
import com.litesuits.orm.db.annotation.Mapping;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.annotation.Table;
import com.litesuits.orm.db.enums.AssignType;
import com.litesuits.orm.db.enums.Relation;
import com.mor.dataacquisition.vo.lowStrain.LowStrain;
import com.mor.dataacquisition.vo.lowStrain.LowStrainData;
import com.rocksea.app.Account;
import com.rocksea.app.entry.DyData;
import com.rocksea.app.http.HttpUtil;
import com.rocksea.app.utils.PerUtils;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Formatter;
import java.util.List;
import java.util.UUID;

/**
 * Created by Mrw on 2017/8/5.
 *
 */
@Table("LowStrainDb")
public class LowStrainDb {
    @PrimaryKey(AssignType.AUTO_INCREMENT)
    @Column("id")
    public int id;

    public String uuid;
    @Default("0")
    public int isLoad;
    public long loadTime;
    public long revicedTime;
    @Ignore
    public String randomCode;
    @Ignore
    public String account;
   // @Ignore
    public long inspectionFormId;
    public String inspectionFormNum;
    public long pileId;

    public String vendorId;
    public String machineId;
    public String serialNo;
    public String pileNo;
    public String fileName;
    public String startTime;
    public double pileDiameter;
    public int pileLength;
    public int velocity;
    public int sampleNumber;
    public String customParam;
    public int validDataCount;          //待定字段
    public String coordinateX;
    public String coordinateY;

    public String ConcreteStrength;
    public byte GpsIsValid;
    public double GpsLongitude;
    public double GpsLatitude;

    @Default("0")
    public long upLoadTime;
    public long creatTime;

    @Default("-1")
    public int serviceId;

    @Mapping(Relation.OneToMany)
    public ArrayList<LowStrainDataDb> lowStrainDataDb;


    public LowStrainDb()
    {
    }
    public LowStrainDb(String lowStrain) {
        DyData mDyData = JSON.parseObject(lowStrain, DyData.class);
        vendorId = mDyData.VendorID;
        machineId = mDyData.MachineID;
        serialNo = mDyData.SerialNo;
        pileNo = mDyData.PileNo;
        pileId = mDyData.pileId;
        inspectionFormId = mDyData.inspectionFormId;
        inspectionFormNum = mDyData.inspectionFormNum;

        //转换桩径
        String sdiameter = mDyData.PileDiameter;
        //String diameter = sdiameter.substring(1, mDyData.PileDiameter.length());
        pileDiameter = Integer.parseInt(sdiameter);
        validDataCount = 4;
        pileLength = (int) mDyData.PileLength;
        velocity = mDyData.Velocity;
        sampleNumber = mDyData.SampleNumber;
        customParam = mDyData.CustomParam;
        startTime = mDyData.StartTime;
        fileName = mDyData.FileName;
        uuid = mDyData.BaseInfoId;

        ConcreteStrength = mDyData.ConcreteStrength;
        coordinateX = HttpUtil.encyptDES(mDyData.coordinateX + "");
        coordinateY = HttpUtil.encyptDES(mDyData.coordinateY + "");

        GpsIsValid = mDyData.GpsIsValid;
        GpsLongitude = mDyData.coordinateX;
        GpsLatitude = mDyData.coordinateY;
        creatTime = new Date().getTime();
        ArrayList<LowStrainDataDb> mLowStrainDataDbs = new ArrayList<>();

        if (mDyData.Data!=null)
        {
            for (int i = 0; i < mDyData.Data.size(); i++) {
                LowStrainDataDb mLowStrainDataDb = new LowStrainDataDb();
                DyData.DataBean mNode = mDyData.Data.get(i);
                mLowStrainDataDb.baseInfoId = mNode.BaseInfoId;
                mLowStrainDataDb.uuid = mNode.uuid;
                mLowStrainDataDb.curNumber = mNode.CurNumber;
                mLowStrainDataDb.delayPoints = mNode.DelayPoints;
                mLowStrainDataDb.integralFlag = mNode.IntegralFlag;
                mLowStrainDataDb.curValidData = i;
                mLowStrainDataDb.lowPass = mNode.LowPass;
                mLowStrainDataDb.highPass = mNode.HighPass;
                mLowStrainDataDb.pointNo = 1;   //TODO 未知
                mLowStrainDataDb.curNumber = mNode.CurNumber;
                mLowStrainDataDb.delayPoints = mNode.DelayPoints;
                mLowStrainDataDb.nChnGain = mNode.nChnGain;
                mLowStrainDataDb.samplingInterval = (int) mNode.SamplingInterval;
                mLowStrainDataDb.sensitivity = mNode.Sensitivity;
                mLowStrainDataDb.samplePoints = mNode.SamplePoints;
                mLowStrainDataDb.sensorType = mNode.SensorType;
                mLowStrainDataDb.ChannelData = mNode.ChannelData;
                mLowStrainDataDb.SampleLength = mNode.SampleLength;
                mLowStrainDataDb.FilterFrequency = mNode.FilterFrequency;
                mLowStrainDataDb.sampleTime = mNode.SampleTime;
                String[] WaveDataArray = mNode.WaveData.split(",");
                mLowStrainDataDb.waveData = new Float[WaveDataArray.length];
                for (int i1 = 0; i1 < WaveDataArray.length; i1++)
                {
                    mLowStrainDataDb.waveData[i1] = Float.parseFloat(WaveDataArray[i1]);
                }

                mLowStrainDataDb.pileBot = 0;
                mLowStrainDataDb.pileTop = 0;
                mLowStrainDataDb.creatTime = new Date().getTime();
                mLowStrainDataDbs.add(mLowStrainDataDb);
            }
            lowStrainDataDb = mLowStrainDataDbs;
        }

    }

    @Table("LowStrainDataDb")
    public static class LowStrainDataDb {
        @PrimaryKey(AssignType.AUTO_INCREMENT)
        @Column("id")
        private int id;
        @Ignore
        public String randomCode;
        @Ignore
        public String account;
        @Ignore
        public long lowStrainId;
        @Ignore
        public long pileId;

        public String baseInfoId;
        public int curValidData;
        public String uuid;
        public int pointNo;
        public int curNumber;
        public String sampleTime;
        public int nChnGain;
        public int highPass;
        public int lowPass;
        public int integralFlag;
        public int sensorType;
        public int samplingInterval;
        public double sensitivity;
        public int pileTop;
        public int pileBot;
        public int delayPoints;
        public int samplePoints;
        @Default("0")
        public byte isLoad;
        public Float[] waveData;
        @Default("0")
        public long upLoadTime;
        @Default("0")
        public byte isValid;  // 0 是有效 1 是无效

        public long creatTime = new Date().getTime();
        public String ChannelData;
        public int SampleLength;
        public int FilterFrequency;

        public LowStrainDataDb()
        {

        }
        public LowStrainDataDb(String json)
        {
            DyData.DataBean mNode = JSON.parseObject(json,DyData.DataBean.class);

            baseInfoId = mNode.BaseInfoId;
            uuid = mNode.uuid;
            curNumber = mNode.CurNumber;
            delayPoints = mNode.DelayPoints;
            integralFlag = mNode.IntegralFlag;
            curValidData = mNode.CurNumber;
            lowPass = mNode.LowPass;
            highPass = mNode.HighPass;
            pointNo = 1;   //TODO 未知
            curNumber = mNode.CurNumber;
            delayPoints = mNode.DelayPoints;
            nChnGain = mNode.nChnGain;
            samplingInterval = (int) mNode.SamplingInterval;
            sensitivity = mNode.Sensitivity;
            samplePoints = mNode.SamplePoints;
            sensorType = mNode.SensorType;
            ChannelData = mNode.ChannelData;
            SampleLength = mNode.SampleLength;
            FilterFrequency = mNode.FilterFrequency;
            sampleTime = mNode.SampleTime;
            String[] WaveDataArray = mNode.WaveData.split(",");
            waveData = new Float[WaveDataArray.length];
            for (int i1 = 0; i1 < WaveDataArray.length; i1++)
            {
                waveData[i1] = Float.parseFloat(WaveDataArray[i1]);
            }
            creatTime = new Date().getTime();
        }
    }


    public LowStrain getLowStrain(int inspectionFormId,String inspectionFormNum,long pileId)
    {
        LowStrain lowStrain = new LowStrain();
        lowStrain. randomCode = Account.getRandomCode();
        lowStrain. account = Account.getAccount();
        lowStrain. inspectionFormId = inspectionFormId;
        lowStrain. inspectionFormNum =inspectionFormNum;
        lowStrain. pileId = pileId;

        lowStrain. vendorId = vendorId;
        lowStrain. machineId = machineId;
        lowStrain. serialNo = "RSDY";
        lowStrain. pileNo = pileNo;
        lowStrain. fileName = fileName;
        lowStrain. startTime = startTime;
        lowStrain. pileDiameter = (int) pileDiameter;
        lowStrain. pileLength = pileLength;
        if (velocity > 9999)
            lowStrain. velocity = 9999;
        else
            lowStrain. velocity = velocity;
        lowStrain. sampleNumber = 99;
        lowStrain. customParam = new JSONObject().toString();
        lowStrain. validDataCount = validDataCount;
        lowStrain.validDataCount = 99;
        lowStrain. coordinateX = coordinateX;
        lowStrain. coordinateY = coordinateY;
        return lowStrain;
    }

    public LowStrainData getBJTKStarinData(long lowStrainId,int index)
    {
        LowStrainDataDb mLowStrainDataDb = lowStrainDataDb.get(index);
        LowStrainData lowStrainData = new LowStrainData();
        lowStrainData.randomCode = Account.getRandomCode();
        lowStrainData.account = Account.getAccount();
        lowStrainData.lowStrainId = lowStrainId;
        lowStrainData.pileId = mLowStrainDataDb.pileId;
        lowStrainData.curValidData = mLowStrainDataDb.curNumber;
        lowStrainData.uuid = mLowStrainDataDb.uuid.replace("-", "");
        lowStrainData.pointNo = mLowStrainDataDb.pointNo;
        lowStrainData.curNumber = mLowStrainDataDb.curNumber;
        //lowStrainData.curNumber = 99;
        lowStrainData.sampleTime = mLowStrainDataDb.sampleTime;
        if (mLowStrainDataDb.nChnGain > 999)
            lowStrainData.nChnGain = 999;
        else
            lowStrainData.nChnGain = mLowStrainDataDb.nChnGain;

        if (mLowStrainDataDb.highPass > 99999)
            lowStrainData.highPass = 99999;
        else
            lowStrainData.highPass = mLowStrainDataDb.highPass;
        if (mLowStrainDataDb.lowPass > 99999)
            lowStrainData.lowPass = 99999;
        else
            lowStrainData.lowPass = mLowStrainDataDb.lowPass;

        lowStrainData.integralFlag = mLowStrainDataDb.integralFlag;
        lowStrainData.sensorType = mLowStrainDataDb.sensorType;
        lowStrainData.samplingInterval = mLowStrainDataDb.samplingInterval;

        lowStrainData.sensitivity = PerUtils.getTwoData(mLowStrainDataDb.sensitivity);
        lowStrainData.pileTop = 1;
        lowStrainData.pileBot = 1;
        if (mLowStrainDataDb.delayPoints > 999)
            lowStrainData.delayPoints = 999;
        else
            lowStrainData.delayPoints = mLowStrainDataDb.delayPoints;

        lowStrainData.samplePoints = mLowStrainDataDb.samplePoints;

        List<Float> waveDataList = new ArrayList<>();
        //Collections.addAll(waveDataList, mLowStrainDataDb.waveData);
        DecimalFormat decimalFormat = new DecimalFormat("#.000000");
        for (int i1 = 0; i1 < mLowStrainDataDb.waveData.length; i1++)
        {
            waveDataList.add(Float.parseFloat(decimalFormat.format(mLowStrainDataDb.waveData[i1])));
        }
        lowStrainData.waveData = waveDataList;

        return lowStrainData;
    }


    public ArrayList<LowStrainData> getStrainData(long lowStrainId)
    {
        ArrayList<LowStrainData> lowStrainDatas = new ArrayList<>();
        for (int i = 0; i < lowStrainDataDb.size(); i++)
        {
            LowStrainDataDb mLowStrainDataDb = lowStrainDataDb.get(i);
            LowStrainData lowStrainData = new LowStrainData();

            lowStrainData.randomCode = Account.getRandomCode();
            lowStrainData.account = Account.getAccount();

            lowStrainData.lowStrainId = lowStrainId;
            lowStrainData.pileId = mLowStrainDataDb.pileId;

            lowStrainData.curValidData = mLowStrainDataDb.curNumber;
            lowStrainData.uuid = mLowStrainDataDb.uuid.replace("-", "");
            lowStrainData.pointNo = mLowStrainDataDb.pointNo;
            lowStrainData.curNumber = mLowStrainDataDb.curNumber;
            lowStrainData.sampleTime = mLowStrainDataDb.sampleTime;
            lowStrainData.nChnGain = mLowStrainDataDb.nChnGain;
            lowStrainData.highPass = mLowStrainDataDb.highPass;
            lowStrainData.lowPass = mLowStrainDataDb.lowPass;
            lowStrainData.integralFlag = mLowStrainDataDb.integralFlag;
            lowStrainData.sensorType = mLowStrainDataDb.sensorType;
            lowStrainData.samplingInterval = mLowStrainDataDb.samplingInterval;
            lowStrainData.sensitivity = PerUtils.getTwoData(mLowStrainDataDb.sensitivity);
            lowStrainData.pileTop = 1;
            lowStrainData.pileBot = 1;
            lowStrainData.delayPoints = mLowStrainDataDb.delayPoints;
            lowStrainData.samplePoints = mLowStrainDataDb.samplePoints;

            List<Float> waveDataList = new ArrayList<>();
            //Collections.addAll(waveDataList, mLowStrainDataDb.waveData);
            DecimalFormat decimalFormat = new DecimalFormat("#.000000");
            for (int i1 = 0; i1 < mLowStrainDataDb.waveData.length; i1++)
            {
                waveDataList.add(Float.parseFloat(decimalFormat.format(mLowStrainDataDb.waveData[i1])));
            }
            lowStrainData.waveData = waveDataList;
            lowStrainDatas.add(lowStrainData);
        }
        return lowStrainDatas;
    }
}
